package com.lrs.java.jdbc.jdbc_exercise;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 
 * @author Administrator
 *
 */
public class Tree_Emp {
	static Connection conn = null; 
	
	public static void tree(int deptno, int grade) {
		String preStr = "";
		String sql = "select * from emp where mgr = " + deptno;
		
		for(int i=0; i<grade; i++) {
			preStr += "****";
		}
		Statement stmt = null;
		ResultSet rs = null;
		try {
			 stmt = conn.createStatement();
			 rs = stmt.executeQuery(sql);
			
			while(rs.next()) {
				System.out.println(preStr + rs.getInt("empno") + "---" + rs.getString("ename"));
				tree(rs.getInt("empno"), grade+1);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs != null) {
					rs.close();
					rs = null;
				}
				if(stmt != null) {
					stmt.close();
					stmt = null;
				}
			} catch(SQLException e) {
				e.printStackTrace();
			}
		}	
	}
	
	public static void main(String[] args) {
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ZLPD", "scott", "tiger");
			
			tree(7839, 0);
			
			System.out.println("success!");
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}
